package org.wx.io.netty.tcp.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @description: tcp 解码器
 * @author: wangxun
 * @createDate: 2021/2/22
 * @version: 1.0
 */
@Slf4j
public class TcpDecoder extends ReplayingDecoder {
    private int  count = 0;

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        log.info("TcpDecoder 开始解码...{}",(++this.count));
        byte command = in.readByte();
        int packageLength = in.readInt();
        byte[] content = new byte[packageLength];
        // 读取内容
        in.readBytes(content);

        MyTcpProtocol myTcpProtocol = new MyTcpProtocol();
        myTcpProtocol.setCommand(command);
        myTcpProtocol.setPackageLength(packageLength);
        myTcpProtocol.setContent(content);

        // 传递给下个Handler处理
        out.add(myTcpProtocol);


    }
}
